﻿/**********************************************************
 ●项目：Smartkernel
 ●公司：智能核心信息技术研发中心
 ●作者：曹艳白
 ●邮箱：caoyanbai@gmail.com
 ●创建：2008-08-10
**********************************************************/
using System.IO;
using System.IO.Compression;

namespace Smartkernel.IO
{
    /// <summary>
    /// GZip格式压缩解压：提供对GZip格式压缩解压操作
    /// </summary>
    public static class SmartGZip
    {
        /// <summary>
        /// 缓存的大小
        /// </summary>
        private static int BUFFER_SIZE = 10000;
        /// <summary>
        /// 压缩流
        /// </summary>
        /// <param name="input">需要被压缩的流</param>
        /// <returns>压缩之后的流</returns>
        public static Stream Compress(Stream input)
        {
            MemoryStream output = new MemoryStream();
            using (GZipStream gzipStream = new GZipStream(output, CompressionMode.Compress, true))
            {
                byte[] buffer = new byte[BUFFER_SIZE];
                int count = input.Read(buffer, 0, BUFFER_SIZE);
                while (count > 0)
                {
                    gzipStream.Write(buffer, 0, count);
                    count = input.Read(buffer, 0, BUFFER_SIZE);
                }
                gzipStream.Flush();
            }
            output.Position = 0;
            return output;
        }
        /// <summary>
        /// 解压流
        /// </summary>
        /// <param name="input">需要被解压的流</param>
        /// <returns>解压之后的流</returns>
        public static Stream Decompress(Stream input)
        {
            MemoryStream output = new MemoryStream();
            using (GZipStream gzipStream = new GZipStream(input, CompressionMode.Decompress, true))
            {
                byte[] buffer = new byte[BUFFER_SIZE];
                int count = gzipStream.Read(buffer, 0, BUFFER_SIZE);
                while (count > 0)
                {
                    output.Write(buffer, 0, count);
                    count = gzipStream.Read(buffer, 0, BUFFER_SIZE);
                }
            }
            output.Position = 0;
            return output;
        }
        /// <summary>
        /// 压缩文件：被压缩的文件必须存在，压缩之后保存的文件路径必须不存在
        /// </summary>
        /// <param name="sourceFilePath">需要被压缩的文件</param>
        /// <param name="targetFilePath">压缩之后保存的文件路径</param>
        public static void Compress(string sourceFilePath, string targetFilePath)
        {
            using (Stream input = File.Open(sourceFilePath, FileMode.Open), output = Compress(input), fileStream = File.Create(targetFilePath))
            {
                byte[] data = new byte[output.Length];
                output.Read(data, 0, data.Length);
                fileStream.Write(data, 0, data.Length);
            }
        }
        /// <summary>
        /// 解压缩文件：被解压缩的文件必须存在，解压之后保存的文件路径必须不存在
        /// </summary>
        /// <param name="sourceFilePath">需要被解压的文件</param>
        /// <param name="targetFilePath">解压之后保存的文件路径</param>
        public static void Decompress(string sourceFilePath, string targetFilePath)
        {
            using (Stream input = File.Open(sourceFilePath, FileMode.Open), output = Decompress(input), fileStream = File.Create(targetFilePath))
            {
                byte[] data = new byte[output.Length];
                output.Read(data, 0, data.Length);
                fileStream.Write(data, 0, data.Length);
            }
        }
    }
}
